package listenTogether;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/listenTogether.MusicControlServlet")
public class MusicControlServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取当前播放的歌曲及其开始时间
        String currentSong = MusicState.getCurrentSong();
        long startTime = MusicState.getStartTime();

        response.setContentType("application/json");
        response.getWriter().write("{\"song\": \"" + currentSong + "\", \"startTime\": " + startTime + "}");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 更新当前播放的歌曲信息
        String song = request.getParameter("song");
        long startTime = System.currentTimeMillis();

        MusicState.setCurrentSong(song, startTime);
        response.getWriter().write("播放状态已更新");
    }
}
